package steg.img;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import bitmap.BMPSave;
import bitmap.BMPobj;

public class StegNewImg
{
	protected String path;
	protected int deep=0;

	public StegNewImg(String path)
	{
		this.path=path;
		deep=0;
	}

	public static void main(String[] args) throws IOException
	{
	
		System.out.println("*** Start ***");
		String root="C:/tmp/test/divina/txt/";
		//String root="C:/tmp/test/";
		StegNewImg stegNewImg = new StegNewImg(root);
		//StegNewImg stegNewImg = new StegNewImg("C:/WorkSpaceDM_Diego/");
		stegNewImg.process();
		System.out.println("*** end ***");
	}

	private void process() throws IOException
	{
		ArrayList<String> listFile= retriveList(path+"/");
		System.out.println("Found : " + listFile.size()+" Files");
		StringBuilder blob = blobizeIt(listFile);
		System.out.println("chars : " + blob.length());
		stegIt(blob);
	}

	private void stegIt(StringBuilder blob)
	{
		int size=(int) (Math.sqrt(blob.length()/3)+1);
		System.out.println("size: "+size);
		if (size>4500){
			System.out.println("too big");
		}
		int lenght=size;
		int width=lenght;
		int[][] blobImg=  new int[size][size];
		int trashold=blob.length()/10;
		for(int index =0; index <blob.length()-3;index ++){
			int row=(index/3)/size;
			int col=(index/3)%size;
			char ch1 = blob.charAt(index++);
			char ch2 = blob.charAt(index++);
			char ch3 = blob.charAt(index);
			int color = (ch1) | (ch2) << 8 | (ch3) << 16;
			//System.out.println("["+row+","+col+"]\t"+(ch1+""+ch2+""+ch3) + "\t"+(int)ch1+" "+(int)ch2 +" "+(int)ch3+"\t"+color);
			blobImg[row][col]=color;
			if(trashold!=0 && index%trashold==0)
				System.out.println("Steg : " + index +" chars");
		}
		

		
		BMPobj img = new  BMPobj(blobImg);
		BMPSave bmp = new BMPSave();
		String path="c:/tmp/test_divina.bmp";
		bmp.saveBMP(path, img.image);
		
		
	}

	protected StringBuilder blobizeIt(ArrayList<String> listFile) throws IOException
	{
		int count=0;
		int trashold=listFile.size()/10;
		StringBuilder res = new StringBuilder();
		for(String f : listFile){
			count++;
			res.append(getStringBuffer(f));
			if(trashold!=0 && count%trashold==0)
				System.out.println("scann : " + count +" files");
		}
		return res.append("###end###");
	}

	private StringBuilder getStringBuffer(String path) throws IOException
	{
		StringBuilder res= new StringBuilder("<<!"+path+"!>>");
		FileInputStream fstream = new FileInputStream(path);
		DataInputStream in = new DataInputStream(fstream);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String strLine;
		while ((strLine = br.readLine()) != null)
		{
			res.append(strLine);
			res.append("\n");
		}
		in.close();
		
		return res;
	}

	protected ArrayList<String> retriveList(String path)
	{
		deep++;
		System.out.println("Deep: "+ deep+" Path:"+path);
		ArrayList<String> res = new ArrayList<String>();
		File dir = new File(path);
		String[] fileList = dir.list();
		for(String f : fileList)
		{
			//System.out.println(path+f);
			File subdir = new File(path+f);
			if(subdir.isDirectory() && deep<20){
				res.addAll(retriveList(path+f+"/"));
				deep--;
			}
			if (accept(f))
				res.add(path+f);
				
		}
		return res;
	}

	private boolean accept(String f)
	{
		return true;
		//System.out.println(f);
//		if(f.endsWith(".txt"))
//			return true;
//		if(f.endsWith(".java"))
//			return true;
//		if(f.endsWith(".csv"))
//			return true;
//		if(f.endsWith(".xml"))
//			return true;
//		if(f.endsWith(".class"))
//			return true;
//		return false;
	}
}
